from django.db.models import QuerySet
from django.http import HttpResponse, QueryDict
from django.views import View
from django.shortcuts import render
import article.models as models
from article.models import Article, Comments


class ArticleView(View):
    def get(self, request):
        data = {
            "title": "article",
            "layui_this": "layui-this",
        }
        article_query_set = models.Article.objects.all()
        article_category = models.ArticleCategory.objects.all()
        return render(request, 'content/article_content.html', locals())

    def post(self, request):
        ...


class ArticleDetailView(View):
    def get(self, request, id):
        data = {
            "title": "article",
            "layui_this": "layui-this",
        }
        article_detail = models.Article.objects.filter(id=id)
        article_category = models.ArticleCategory.objects.all()
        for i in article_detail:
            detail = i
        user_id = request.session.get('user_id', '')
        comments = models.Comments.objects.filter(article_id=id)
        return render(request, 'content/detail_content.html', locals())

    def post(self, request, id):
        params: QueryDict
        params = request.POST
        comments_content = params.get("content", "")
        user_id = request.session.get('user_id','')
        Comments.objects.create(create_user_id=user_id, article_id=user_id, comments_content=comments_content)
        return HttpResponse(f"添加成功 <a href='/artcle/detail/{id}'>点击返回文章</a>")
